//
//  MOLaderEvaluationDetailViewController.m
//  MobileOffice
//
//  Created by 冰冰 on 2019/8/16.
//  Copyright © 2019 com.idolmedia. All rights reserved.
//

#import "MOLaderEvaluationDetailViewController.h"
#import "MOLaderEvaluationDetailModel.h"

@interface MOLaderEvaluationDetailViewController ()<UITableViewDelegate,UITableViewDataSource>

@property(nonatomic,strong)UITableView *tableView;

@property(nonatomic,strong)UIView *bottomView;

@property(nonatomic,strong)NSMutableArray *dataSource;

@end

@implementation MOLaderEvaluationDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self creatView];
    
    [self initData];
}

-(void)creatView
{
    self.title = @"领导评鉴";
    
    self.view.backgroundColor = KWhiteColor;
    
    [self.view addSubview:self.tableView];
    
    [self.view addSubview:self.bottomView];
}

-(void)initData
{
    NSString *gradeStr = @"";
    switch ([[NSString passNullForString:self.listModel.fenZhiStr] integerValue]) {
        case 50:
            gradeStr = @"差";
            break;
        case 70:
            gradeStr = @"一般";
            break;
        case 90:
            gradeStr = @"较好";
            break;
        case 100:
            gradeStr = @"好";
            break;
            
        default:
            break;
    }
    NSString *jiDu = @"";
    if ([self.jiDuStr isEqualToString:@"Q01"]) {
        jiDu = @"第一季度";
    }else if ([self.jiDuStr isEqualToString:@"Q03"]){
        jiDu = @"第二季度";
    }else if ([self.jiDuStr isEqualToString:@"Q03"]){
        jiDu = @"第三季度";
    }else{
        jiDu = @"第四季度";
    }
    
    self.dataSource = [NSMutableArray array];
    NSArray *arr = @[@"年度\t\t",@"季度\t\t",@"人员姓名\t",@"评鉴等级\t",@"评鉴分值\t"];
    NSArray *vArr = @[[NSString stringWithFormat:@"%@年",self.dateStr],jiDu,self.listModel.nameStr,gradeStr,self.listModel.fenZhiStr];
    for (int i = 0; i<arr.count; i++) {
        MOLaderEvaluationDetailModel *model = [[MOLaderEvaluationDetailModel alloc]init];
        model.nameStr = arr[i];
        model.valueStr = vArr[i];
        [self.dataSource addObject:model];
    }
    [self.tableView reloadData];
}


#pragma mark -- 提交
-(void)clickSubmitButton
{
    if ([self.listModel.fenZhiStr integerValue]==0) {
        [[ADShowMessage shareShowMessage]showTextOnly:@"请选择评鉴等级" showInView:self.view];
        return;
    }
    MOHintView *hintView = [[MOHintView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT) andContentStr:@"确定保存？"];
    __weak typeof (self)weakSelf = self;
    hintView.clickSureButtonBlock = ^{
        [weakSelf seaveData];
    };
    [WINDOW addSubview:hintView];
}

-(void)seaveData
{
    NSString *url = [NSString stringWithFormat:@"%@?authorization=%@",SZRSLDPJ_SAVELDPJ_URL,[NSString URLEncodedString:[MOUser sharedMOUser].token]];
    NSDictionary *dic = @{
                          @"nd":self.dateStr,
                          @"jd":self.jiDuStr,
                          @"oidStr":[NSString stringWithFormat:@"%@,%@,%@",self.listModel.idStr,@"",self.listModel.fenZhiStr],
                          };
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    [PPNetworkHelper POST:url parameters:@{@"param":[NSString dictionaryToJson:dic]} success:^(id responseObject) {
        NSLog(@"领导评鉴保存：%@",responseObject);
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        if ([responseObject isKindOfClass:[NSDictionary class]]) {
            [[ADShowMessage shareShowMessage]showTextOnly:responseObject[@"msg"] showInView:self.view];
            if ([responseObject[@"status"] integerValue]==1) {
                if (self.saveSucessBlock) {
                    self.saveSucessBlock();
                }
            }
        }
    } failure:^(NSError *error) {
        NSLog(@"领导评鉴保存：%@",[error localizedDescription]);
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        [[ADShowMessage shareShowMessage]showTextOnly:[error localizedDescription] showInView:self.view];
    }];
}


#pragma mark -- UITableViewDelegate,UITableViewDataSource
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataSource.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (!cell) {
        cell = [[UITableViewCell alloc]initWithStyle:(UITableViewCellStyleDefault) reuseIdentifier:@"cell"];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        cell.textLabel.textColor = KBlackColor_222222;
        cell.textLabel.font = FONT_SIZE(15);
    }
    
    if (indexPath.row==3) {
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }else{
        cell.accessoryType = UITableViewCellAccessoryNone;
    }
    
    MOLaderEvaluationDetailModel *model = self.dataSource[indexPath.row];
    cell.textLabel.attributedText = [NSString contentString:[NSString stringWithFormat:@"%@%@",model.nameStr,model.valueStr] subString:model.valueStr subColor:KGrayColor_919395 subFont:FONT_SIZE(15)];
    
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return kHConversions(54);
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.8;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 13;
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    return nil;
}

-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    return nil;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row==3) {
        
        MOLaderEvaluationDetailModel *model = self.dataSource[indexPath.row];
        SNXYSelectedNumberView *pickview = [[SNXYSelectedNumberView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
        NSArray *tArr = @[@"好",@"较好",@"一般",@"差"];
        NSArray *vArr = @[@"100",@"90",@"70",@"50"];
        pickview.numberArray = tArr;
        __weak typeof (self)weakSelf = self;
        pickview.clickIndexBlock = ^(NSInteger index) {
            
            model.valueStr = tArr[index];
            
            MOLaderEvaluationDetailModel *model1 = self.dataSource[indexPath.row+1];
            model1.valueStr = vArr[index];
            
            [weakSelf.tableView reloadData];
            
            weakSelf.listModel.fenZhiStr = vArr[index];
            
            weakSelf.listModel.gradeStr = tArr[index];
            
        };
        [WINDOW addSubview:pickview];
        
    }
}

#pragma mark - 设置顶头分割线
-(void)viewDidLayoutSubviews {
    
    if ([self.tableView respondsToSelector:@selector(setSeparatorInset:)]) {
        [self.tableView setSeparatorInset:UIEdgeInsetsMake(0, 0, 0, 0)];
    }
    if ([self.tableView respondsToSelector:@selector(setLayoutMargins:)])  {
        if (@available(iOS 8.0, *)) {
            [self.tableView setLayoutMargins:UIEdgeInsetsMake(0, 0, 0, 0)];
        } else {
            // Fallback on earlier versions
        }
    }
}

-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPat{
    if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
        if (@available(iOS 8.0, *)) {
            [cell setLayoutMargins:UIEdgeInsetsMake(0, 0, 0, 0)];
        } else {
            // Fallback on earlier versions
        }
    }
    if ([cell respondsToSelector:@selector(setSeparatorInset:)]){
        [cell setSeparatorInset:UIEdgeInsetsMake(0, 0, 0, 0)];
    }
}


#pragma mark -懒加载
-(UITableView *)tableView
{
    if (!_tableView) {
        
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT-StatusBarAndNavigationBarHeight - 57 - TabbarSafeBottomMargin) style:UITableViewStyleGrouped];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.backgroundColor = KGrayColor_ededed;
        _tableView.showsVerticalScrollIndicator = NO;
        
    }
    return _tableView;
}

-(UIView *)bottomView
{
    if (!_bottomView) {
        _bottomView = [[UIView alloc]initWithFrame:CGRectMake(0, SCREEN_HEIGHT - StatusBarAndNavigationBarHeight - TabbarSafeBottomMargin - 57, SCREEN_WIDTH, 57)];
        
        UIButton *submitButton = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, _bottomView.width, _bottomView.height)];
        [submitButton setTitle:@"提交" forState:UIControlStateNormal];
        [submitButton setTitleColor:KBlueColor_2C94FF forState:UIControlStateNormal];
        submitButton.titleLabel.font = FONT_SIZE(15);
        [submitButton addTarget:self action:@selector(clickSubmitButton) forControlEvents:UIControlEventTouchUpInside];
        [_bottomView addSubview:submitButton];
    }
    return _bottomView;
}

@end
